<?php

namespace app\crm\model;

use think\Db;

class SubregionSys extends \ShopModel
{
    protected $table = 'sh_crm_subregion_sys';
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    //修改可见权限
    function subregionVisible($data){
        $subregion_id = $where['subregion_id'] = $up['subregion_id'] = $data['subregion_id'];
        $is_Add =  $this->where($where)->count();

        if(isset($data['is_staff'])&&$data['is_staff']!=''){

            $up['is_staff_all_visible'] = $data['is_staff'];
        }

        if(isset($data['is_downstream'])&&$data['is_staff']!=''){
            $up['is_downstream_all_visible'] = $data['is_downstream'];
        }
        if(isset($data['staff_id'])&&!empty($data['staff_id'])){
            $this->delStaff($subregion_id);
            foreach ($data['staff_id'] as $v){
                $addStaff['staff_id'] =  $v;
                $addStaff['subregion_id'] =  $subregion_id;
                Db::name('crm_subregion_visible_staff')->insert($addStaff);
            }
        }
        if(isset($data['downstream_id'])&&!empty($data['downstream_id'])){
            $this->delDownstream($subregion_id);
            foreach ($data['downstream_id'] as $v1){
                $addDownstream['downstream_id'] =  $v1;
                $addDownstream['subregion_id'] =  $subregion_id;
                Db::name('crm_subregion_visible_downstream')->insert($addDownstream);
            }
        }
        if($is_Add){

            $this->where($where)->update($up);
        }else{
            $this->insert($up);
        }

    }
    //删除所有分区可见员
    public function delStaff($subregion_id){
        Db::name('crm_subregion_visible_staff')->where('subregion_id',$subregion_id)->delete();
    }
    //删除所有分区下游可见人员
    public function delDownstream($subregion_id){
        Db::name('crm_subregion_visible_downstream')->where('subregion_id',$subregion_id)->delete();
    }
    //获取可见权限
    public function info($subregion_id){
       $data = $this->where('subregion_id',$subregion_id)->field('subregion_id,is_staff_all_visible,is_downstream_all_visible')->find();

       if(!empty($data)){

           if($data['is_staff_all_visible']==1){
               $data['subregion_visible_staff'] =  Db::name('crm_subregion_visible_staff')->alias('a')->join('sh_crm_staff b','a.staff_id = b.staff_id')->where('a.subregion_id',$subregion_id)->field('a.staff_id,b.staff_name')->select();
           }
           if($data['is_downstream_all_visible']==2){
               $data['subregion_visible_downstream'] =  Db::name('crm_subregion_visible_downstream')->alias('a')->join('sh_crm_staff b','a.downstream_id = b.staff_id')->where('a.subregion_id',$subregion_id)->field('a.downstream_id,b.staff_name as downstream_name')->select();
           }
           $data['is_shezi'] = 1;
       }else{
           $data['is_shezi'] = 0;
       }

        return $data;

    }
    //修改发布权
    function subregionIssue($data){
        $subregion_id = $where['subregion_id'] = $up['subregion_id'] = $data['subregion_id'];
        $is_Add =  $this->where($where)->count();

        if(isset($data['is_staff_issue'])&&$data['is_staff_issue']!=''){
            $up['is_staff_issue'] = $data['is_staff_issue'];
        }

        if(isset($data['is_downstream_issue'])&&$data['is_downstream_issue']!=''){
            $up['is_downstream_issue'] = $data['is_downstream_issue'];
        }
        if(isset($data['staff_id'])&&!empty($data['staff_id'])){
            $this->delIssueStaff($subregion_id);
            foreach ($data['staff_id'] as $v){
                $addStaff['staff_id'] =  $v;
                $addStaff['subregion_id'] =  $subregion_id;
                Db::name('crm_subregion_issue_staff')->insert($addStaff);
            }
        }

        if($is_Add){

            $this->where($where)->update($up);
        }else{
            $this->insert($up);
        }

    }
    //删除所有分区可见员
    public function delIssueStaff($subregion_id){
        Db::name('crm_subregion_issue_staff')->where('subregion_id',$subregion_id)->delete();
    }
    //获取发布权权限
    public function infoIssue($subregion_id){
        $data = $this->where('subregion_id',$subregion_id)->field('subregion_id,is_staff_issue,is_downstream_issue')->find();
        if(!empty($data)){
            if($data['is_staff_issue']==1){
                $data['subregion_issue_staff'] =  Db::name('crm_subregion_issue_staff')->alias('a')->join('sh_crm_staff b','a.staff_id = b.staff_id')->where('a.subregion_id',$subregion_id)->field('a.staff_id,b.staff_name')->select();
            }
                $data['is_shezi'] = 1;
        }else{
            $data['is_shezi'] = 0;
        }

        return $data;

    }
    //分社区联系人设置
    function subregionLinkman($data){
        $subregion_id = $where['subregion_id'] = $up['subregion_id'] = $data['subregion_id'];
        $is_Add =  $this->where($where)->count();
        $up['linkman_phone'] = $data['linkman_phone'];
        $up['linkman_email'] = $data['linkman_email'];
        $up['linkman_telephone'] = $data['linkman_telephone'];
        $up['linkman_wechat'] = $data['linkman_wechat'];
        $up['linkman_qq'] = $data['linkman_qq'];
        if(isset($data['staff_id'])&&!empty($data['staff_id'])){
            $this->infoLinkman($subregion_id);
            foreach ($data['staff_id'] as $v){
                $addStaff['staff_id'] =  $v;
                $addStaff['subregion_id'] =  $subregion_id;
                Db::name('crm_subregion_linkman_staff')->insert($addStaff);
            }
        }
        if($is_Add){
            $this->where($where)->update($up);
        }else{
            $this->insert($up);
        }

    }
    //获取分社区联系人设置
    public function infoLinkman($subregion_id){
        $data = $this->where('subregion_id',$subregion_id)->field('linkman_phone,linkman_email,linkman_telephone,linkman_wechat,linkman_qq')->find();
        if(!empty($data)){
            $data['subregion_issue_linkman'] =  Db::name('crm_subregion_linkman_staff')->alias('a')->join('sh_crm_staff b','a.staff_id = b.staff_id')->where('a.subregion_id',$subregion_id)->field('a.staff_id,b.staff_name')->select();
            $data['is_shezi'] = 1;
        }else{
            $data['is_shezi'] = 0;
        }
        return $data;
    }
    //删除所有分区可见员
    public function delLinkmanStaff($subregion_id){
        Db::name('crm_subregion_linkman_staff')->where('subregion_id',$subregion_id)->delete();
    }
    //新增分类
    public function addClassify($data){
        $add['is_access'] = $data['is_access'];
        $add['classify_name'] = $data['classify_name'];
        $add['subregion_id'] = $data['subregion_id'];
        $retData['id'] = Db::name('crm_subregion_classify')->insertGetId($add);
        return $retData;
    }
    //删除分类
    public function delClassify($classify_id){
       $retData['id'] = Db::name('crm_subregion_classify')->where('classify_id',$classify_id)->delete();
    }
    //查询分类列表
    public function listClassify($subregion_id){
        $retData = Db::name('crm_subregion_classify')->where('subregion_id',$subregion_id)->select();
        return $retData;
    }
    //设置需提醒员工
    public function remind($data){
        $subregion_id = $data['subregion_id'];
        if(isset($data['staff_id'])&&!empty($data['staff_id'])){
            $this->delIssueStaff($subregion_id);
            foreach ($data['staff_id'] as $v){
                $addStaff['staff_id'] =  $v;
                $addStaff['subregion_id'] =  $subregion_id;
                Db::name('crm_subregion_remind_staff')->insert($addStaff);
            }
        }
    }
    //查看提醒员工
    public function infoRemind($subregion_id){
       $data =  Db::name('crm_subregion_remind_staff')->alias('a')->join('sh_crm_staff b','a.staff_id = b.staff_id')->where('a.subregion_id',$subregion_id)->field('a.staff_id,b.staff_name')->select();
       return $data;
    }
    //下游点赞、评论互不可见 设置
    public function downstream($data){
        $where['subregion_id'] = $up['subregion_id'] = $data['subregion_id'];
        $is_Add =  $this->where($where)->count();
        $up['is_downstream_access'] = $data['is_downstream_access'];
        if($is_Add){
            $this->where($where)->update($up);
        }else{
            $this->insert($up);
        }
    }
    //下游点赞、评论互不可见 查看
    public function getDownstream($subregion_id){
        $where['subregion_id'] = $subregion_id;
        $retData['is_downstream_access'] =  $this->where($where)->value('is_downstream_access');
        return $retData;
    }
    //下游发布动态默认可见范围 设置
    public function downstreamScope($data){
        $where['subregion_id'] = $up['subregion_id'] = $data['subregion_id'];
        $is_Add =  $this->where($where)->count();
        $up['downstream_scope'] = $data['downstream_scope'];
        if($is_Add){
            $this->where($where)->update($up);
        }else{
            $this->insert($up);
        }
    }
    //下游发布动态默认可见范围 查看
    public function getDownstreamScope($subregion_id){
        $where['subregion_id'] = $subregion_id;
        $retData['downstream_scope'] =  $this->where($where)->value('downstream_scope');
        return $retData;
    }
}